<template>
  <div class="container top-category">
    <XtxBread>
      <XtxBreadItem to="/">首页</XtxBreadItem>
      <Transition name="fade-right" mode="out-in">
        <XtxBreadItem :key="category.id" v-if="category">
          {{ category?.name }}
        </XtxBreadItem>
      </Transition>
    </XtxBread>
    <!-- 轮播图 -->
    <XtxCarousel :sliders="sliders" style="height: 500px;"></XtxCarousel>
    <!-- 全部分类 -->
    <!-- 所有二级分类 -->
    <div class="sub-list">
      <h3>全部分类</h3>
      <ul style="height: 160px;">
        <li v-for="item in category?.children" :key="item.id">
          <RouterLink :to="`/category/sub/${item.id}`">
            <img :src="item.picture" />
            <p>{{ item.name }}</p>
          </RouterLink>
        </li>
      </ul>
    </div>

    <!-- 分类关联商品 -->
    <div class="ref-goods" v-for="item in categoryList" :key="item.id">
      <div class="head">
        <h3>- {{ item.name }} -</h3>
        <p class="tag">温暖柔软，品质之选</p>
        <XtxMore />
      </div>
      <div class="body">
        <GoodsItem v-for="goods in item.goods" :key="goods.id" :goods="goods" />
      </div>
    </div>
  </div>
</template>

<script>
import { computed, ref, watch } from 'vue'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { findBanner } from '@/api/home'
import GoodsItem from './components/goods-item.vue'
import { findTopCategory } from '@/api/category'
export default {
  name: 'TopCategory',
  components: {
    GoodsItem
  },
  setup() {
    // 1. 从vuex中获取所有的分类数据
    const store = useStore()
    const route = useRoute()
    const category = computed(() => {
      return store.state.category.list.find(item => item.id === route.params.id)
    })

    // 渲染轮播图结构
    const sliders = ref([])
    findBanner().then(res => {
      sliders.value = res.result
    })

    // 顶级分类下的二级列表
    const categoryList = ref([])

    watch(
      () => route.params.id,
      value => {
        // 如果此时跳转的是二级路由，不需要发送请求
        if (route.path.includes('/sub')) return
        // 如果我们路由发生了变化，但是如果此时不是一级路由，就不应该发送请求了
        findTopCategory(value).then(res => {
          categoryList.value = res.result.children
        })
      },
      {
        immediate: true
      }
    )
    return {
      category,
      sliders,
      categoryList
    }
  }
}
</script>
<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
}
.ref-goods {
  background-color: #fff;
  margin-top: 20px;
  position: relative;
  .head {
    .xtx-more {
      position: absolute;
      top: 20px;
      right: 20px;
    }
    .tag {
      text-align: center;
      color: #999;
      font-size: 20px;
      position: relative;
      top: -20px;
    }
  }
  .body {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    padding: 0 65px 30px;
  }
}
</style>
